//
//  ViewFactory.m
//  工厂模式
//
//  Created by macOS on 2017/4/20.
//  Copyright © 2017年 macOS. All rights reserved.
//

#import "ViewFactory.h"
#import "WSJBtn1.h"
#import "WSJImageView.h"
#import "WSJLabel.h"

#define VIEW_0 @"VIEW_BUTTON"
#define VIEW_1 @"VIEW_IMAGEVIEW"
#define VIEW_2 @"VIEW_LABEL"
static ViewFactory *instance = nil;

@interface ViewFactory ()

@property (nonatomic , strong) NSMutableDictionary *dict;

@end

@implementation ViewFactory

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [ViewFactory new];
    });
    return instance;
}


+ (instancetype)allocWithZone:(struct _NSZone *)zone{
   
    if (instance == nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [ViewFactory new];
        });
    }
    
    return instance;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        _dict =  [NSMutableDictionary dictionary];
        _dict[@"VIEW_0"] = @"WSJBtn1";
        _dict[@"VIEW_1"] = @"VIEW_IMAGEVIEW";
        _dict[@"VIEW_2"] = @"VIEW_LABEL";
        
    }
    return self;
}

- (NSObject *)createView:(NSString *)view_type{
   
    
    //根据类名 动态创建实例对象
    NSString *className = self.dict[view_type];
    
    NSObject *obj = nil;
    if (className) {
        obj = [[NSClassFromString(className) alloc] init];
    }
    
    return obj;

}

//
//+ (NSObject *)createView:(NSInteger)choose{
//  
//    NSObject *obj = nil;
//    switch (choose) {
//        case 0:
//            obj =  [WSJBtn1 new];
//            break;
//        case 1:
//            obj =  [WSJImageView new];
//            break;
//        case 2:
//            obj =  [WSJLabel new];
//            break;
//            
//        default:
//            obj =  [WSJBtn1 new];
//            break;
//    }
//    
//    return  obj;
//}

@end
